﻿using Asion.Dapper.SqlBuilder;
using Asion.Dapper.SqlBuilder.Condition;
using Dapper;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using static Asion.Dapper.SqlMapperExtensions;

namespace Asion.Dapper
{
    public interface IUpdateColumn : ICondition
    {
    }


    public class UpdateColumn : BaseCondition, IUpdateColumn, IConditionDynamicParameters, IGetSql
    {
  

        internal List<ComplexCondtion> Conditions;

        public UpdateColumn()
        {
            Conditions = new List<ComplexCondtion>();
        }

        public UpdateColumn(IDictionary<string, string> columns) : this()
        {
            foreach (var item in columns)
            {
                Set(item.Key, item.Value);
            }
        }

        public UpdateColumn Set(string column, object value, string columnNick = "")
        {
            Conditions.Add(new ComplexCondtion(column, CondtionOperator.Set, value, columnNick));
            return this;
        }


        public UpdateColumn Increase(string column, int value, string columnNick = "")
        {
            Conditions.Add(new ComplexCondtion(column, CondtionOperator.IncreaseSet, value, columnNick));
            return this;
        }

        public UpdateColumn Reduce(string column, int value, string columnNick = "")
        {
            Conditions.Add(new ComplexCondtion(column, CondtionOperator.ReduceSet, value, columnNick));
            return this;
        }

        public string GetSql()
        {
            return BuildSql();
        }

        public override string BuildSql()
        {
            if (Conditions == null || !Conditions.Any())
            {
                throw new ArgumentException("set column is not empty ");
            }
            var sql = new StringBuilder();
            sql.AppendFormat(" SET ");
            for (var i = 0; i < Conditions.Count(); i++)
            {
                var column = Conditions.ElementAt(i);
                var mapping = _custom_table_mappinghandle.GetColumnMapping(column.Column);
                if (mapping == null)
                {
                    throw new ArgumentException($"set column {column.Column} does not exist ");
                }

                var formula = ComplexOperatorFactory.GetComplexCondtionOperatorFormula(column.Operator);
                //使用参数化
                var stringArray = new List<DbType>()                {                    DbType.String,                    DbType.DateTime,                    DbType.Date,                    DbType.DateTime2                };
                var updateParamName = "update_" + mapping.DataColumnName;                sql.AppendFormat(formula, _encap_sulation.EncapSulation(mapping.DataColumnName), _encap_sulation.EncapCloumnParam(updateParamName));
                AddDynamicParam(updateParamName, column.Value, mapping.GetDataColumnType(), column.Operator);
                sql.AppendFormat(", ");
            }
            if (sql.ToString().EndsWith(", "))
                sql.Remove(sql.Length - 2, 2);

            return sql.ToString();
        }

        public DynamicParameters GetDynamicParameters()
        {
            return DynamicParam;
        }
    }












}
